<?php

namespace Home\Controller;

use Think\Controller;

//加载commons下的functions.php自定义函数库
load("@.functions");

class UserController extends Controller {

    public function login() {
        $this->display();
    }

    public function user_list() {
        $User = M('User'); // 实例化User对象
        //进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $list = $User->order('id')->page($_GET['p'] . ',25')->select();
        $this->assign('list', $list); // 赋值数据集
        $count = $User->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show(); // 分页显示输出
        $this->assign('page', $show); // 赋值分页输出
        $this->display(); // 输出模板
    }

    public function userLogin() {
        //\UserApi::userLogin_('umgsai1', '123456');
        //使用A函数来实例化控制器
        //$userApi = A("Api/UserApi");
//        var_dump($userApi);
        //跨模块调用
        //$userApi->userLogin("umgsai1", "123456");
        //简便写法，实例化控制器并且调用方法
        R("Api/UserApi/userLogin('umgsai1', '123456')");
        //跨项目调用
        //$index = A("book://Home/Index");
    }

    /**
     * 用户详细信息
     */
    function user_detail() {
        $id = I('get.id');
        $UserModel = new \Common\Model\UserModel();
        $user = $UserModel->get($id);
//        show_bug($user);
        $this->assign('user', $user);
        session('user_id', $user['id']);  //设置session, 上传头像时url传值出现问题，此为权宜之计
        //查询当前user的好友
        $FriendModel = new \Common\Model\FriendModel();
        $friends = $FriendModel->get_friends($user['id']);
        $this->assign('friends', $friends);
//        show_bug($friends);
//        die();
        $this->display();
    }

    /**
     * 需要验证session的合法性
     */
    function user_upload_icon() {
        header("Content-Type: text/html; charset=utf-8");
        $rs = array();
        $id = session("user_id");
        if ($id == null) {
            $rs['status'] = -2;
            print json_encode($rs);
            return;
            //session过期
        }
        $UserModel = new \Common\Model\UserModel();
        $user = $UserModel->get($id);

        $date = date("Y-m-d H:i:s"); //2015-05-13 16:29:37
        $year = substr($date, 0, 4); //2015
        $month = substr($date, 5, 2); //05
        $day = substr($date, 8, 2); //13
        $dir = './Public/uploads/' . $year . '/' . $month . '/' . $day . '/';
        create_dir($dir); //调用公共函数


        switch ($_GET['action']) {
            //上传临时图片
            case 'uploadtmp':
                $file = 'uploadtmp.jpg';
                @move_uploaded_file($_FILES['Filedata']['tmp_name'], $file);
                $rs['status'] = 1;
                $rs['url'] = './' . $file; //使用的时候这句需要修改
                break;

            //上传切头像
            case 'uploadavatar':
                $input = file_get_contents('php://input');
                $data = explode('--------------------', $input);
//                show_bug($data[0]['type']);
//                die();
                $target_path = $dir . create_uuid("ico_") . '.jpg';
//                @file_put_contents('./avatar_1.jpg', $data[0]);//剪切之后的图
//                @file_put_contents('./avatar_2.jpg', $data[1]);//原图
                @file_put_contents($target_path, $data[0]);

                //①从dic_table中查出user表的table_id
                $Dic_tableModel = new \Common\Model\Dic_tableModel();
                $dic_table = $Dic_tableModel->get_id_by_name(C('DB_PREFIX') . "user");
                //②获取user在user表中的id即为id_in_that_tab
                $table_id = $dic_table['id'];
                $id_in_that_tab = $user['id'];
                $ImageModel = new \Common\Model\ImageModel();
//                echo $id_in_that_tab;
//                show_bug($user);
//                die();
                //上传新的头像之后更新数据库并且删除旧的头像
                $oldImage = $ImageModel->where("table_name = 'dy_user' and id_in_that_tab = " . $id_in_that_tab)->find();
                if ($oldImage == null) {//还没上传过头像,要想image表中insert记录
                    $ImageModel = new \Common\Model\ImageModel();
                    $image['img_name'] = "";
                    $image['img_url'] = $target_path;
                    $image['table_name'] = "dy_user";
                    $image['id_in_that_tab'] = $id_in_that_tab;
                    $image['memo'] = $user['phone'] . "的头像";
                    $ImageModel->add($image);
                    echo $ImageModel->getLastSql();
                } else {//已经上传过头像，要更新image表中的记录
                    $old_url = $oldImage['img_url'];
                    $oldImage['img_url'] = $target_path;
                    $ImageModel->save($oldImage); //更新数据库
                    @chmod($oldImage['img_url'], 0777);
                    if (!unlink($old_url)) {
                        //删除旧头像
                    }
                }

                $rs['status'] = 1;
                break;

            default:
                $rs['status'] = -1;
        }
        print json_encode($rs);
    }

    function admin_list() {
        $admins = D("Admin");
        $total = $admins->count();
//        show_bug($total);
        $pageSize = $_GET['pageSize'];
        if ($pageSize == '' || !is_numeric($pageSize) || $pageSize <= 0) {
            $pageSize = 10;
        }
        $page = new \Component\Page($total, $pageSize);
        $sql = "select * from dy_admin " . $page->limit;
        $info = $admins->query($sql);
        for ($i = 0; $i < count($info); $i++) {
            $info[$i]['password'] = "";
        }
//        $this->assign('page', $page); // 赋值分页输出
        $pageInfo = $page->fpage();
        $this->assign('pageInfo', $pageInfo);
//        show_bug($page);
//        die();
//        show_bug($info);
//        die();
        $this->assign("admins", $info);
        $this->display();
    }

}
